package com.jason.service.impl;

import com.jason.base.BaseServiceImpl;
import com.jason.core.config.jwt.JwtUser;
import com.jason.entity.SysRole;
import com.jason.entity.SysUser;
import com.jason.mapper.SysUserMapper;
import com.jason.service.ISysUserService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author sj
 * @since 2018-04-25
 */
@Service
public class SysUserServiceImpl extends BaseServiceImpl<SysUserMapper, SysUser> implements ISysUserService {

    public JwtUser selectUserRole(String userName){
        JwtUser jwtUser = new JwtUser();
        List<Map<String, Object>> list =  baseMapper.selectUserRole(userName);
        int size = list.size();
        List<SysRole> roles = new ArrayList<>();
        for (int i=0; i<size; i++){
            Map<String, Object> map = list.get(i);
            if (i == 0){
                jwtUser.setId(Long.valueOf(String.valueOf(map.get("id"))));
                jwtUser.setUsername(String.valueOf(map.get("username")));
                jwtUser.setPassword(String.valueOf(map.get("password")));
            }
            SysRole sysRole = new SysRole();
            sysRole.setId(Long.valueOf(String.valueOf(map.get("roleId"))));
            sysRole.setRoleName(String.valueOf(map.get("roleName")));
            roles.add(sysRole);
        }
        jwtUser.setRoles(roles);
        return jwtUser;
    }

}
